package org.insia.smartposse.utils;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.validator.EmailValidator;
import org.apache.log4j.Logger;

/**
 * Class with specific methode for getting parameter from request.
 * @author sok hout
 *
 */
public class ActionUtils {
	protected final static Logger _LOG = Logger.getLogger(ActionUtils.class);

	
	/**
	 * Return Date.
	 * @param request
	 * @param paramName
	 * @return
	 */
	public static Date getDate(HttpServletRequest request, String paramName, Date defaultValue, List<String> messages, String msgKey) {
		Date rst = null;
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
		try {
			rst = sdf.parse(request.getParameter(paramName).toString());
		} catch (Exception e) {
			rst = defaultValue;
			if (messages != null)
				messages.add(msgKey);
			_LOG.info(e);
		}
		return rst;
	}	
	
	/**
	 * Return long.
	 * @param request
	 * @param paramName
	 * @return
	 */
	public static Long getLong(HttpServletRequest request, String paramName, Long defaultValue, List<String> messages, String msgKey) {
		Long rst = null;
		try {
			rst = Long.parseLong(request.getParameter(paramName).toString());
		} catch (Exception e) {
			rst = defaultValue;
			if (messages != null)
				messages.add(msgKey);
			_LOG.info(e);
		}
		return rst;
	}
	
	/**
	 * Returnint.
	 * @param request
	 * @param paramName
	 * @return
	 */
	public static Integer getInt(HttpServletRequest request, String paramName, Integer defaultValue, List<String> messages, String msgKey) {
		Integer rst = null;
		try {
			rst = Integer.parseInt(request.getParameter(paramName).toString());
		} catch (Exception e) {
			rst = defaultValue;
			if (messages != null)
				messages.add(msgKey);
			_LOG.info(e);
		}
		return rst;
	}
	
	/**
	 * Return string.
	 * @param request
	 * @param paramName
	 * @return
	 */
	public static String getString(HttpServletRequest request, String paramName, String defaultValue, List<String> messages, String msgKey) {
		String rst = null;
		try {
			rst = request.getParameter(paramName).toString();
		} catch (Exception e) {
			rst = defaultValue;
			if (messages != null)
				messages.add(msgKey);
			_LOG.info(e);
		}
		return rst;
	}
	
	/**
	 * 
	 * @param request
	 * @param paramName
	 * @param defaultValue
	 * @param messages
	 * @param msgKey
	 * @return
	 */
	public static String getTrimedString(HttpServletRequest request, String paramName, String defaultValue, List<String> messages, String msgKey) {
		String rst = null;
		try {
			rst = request.getParameter(paramName).toString().trim();
			if (rst.length() == 0)
				if (messages != null)
					messages.add(msgKey);
		} catch (Exception e) {
			rst = defaultValue;
			if (messages != null)
				messages.add(msgKey);
			_LOG.info(e);
		}
		return rst;
	}
	/**
	 * Return email.
	 * @param request
	 * @param paramName
	 * @param defaultValue
	 * @param messages
	 * @param msgKey
	 * @return
	 */
	public static String getEmail(HttpServletRequest request, String paramName, String defaultValue, List<String> messages, String msgKey) {
		String rst = null;
		try {
			rst = request.getParameter(paramName).toString();
			if (!EmailValidator.getInstance().isValid(rst))
				throw new Exception("Email is invalide!");
		} catch (Exception e) {
			rst = defaultValue;
			if (messages != null)
				messages.add(msgKey);
			_LOG.info(e);
		}
		return rst;
	}
	
	
	/**
	 * Return double.
	 * @param request
	 * @param paramName
	 * @param defaultValue
	 * @param messages
	 * @param msgKey
	 * @return
	 */
	public static Double getDouble(HttpServletRequest request, String paramName, Double defaultValue, List<String> messages, String msgKey) {
		Double rst = null;
		try {
			rst = Double.parseDouble(request.getParameter(paramName).toString());
		} catch (Exception e) {
			rst = defaultValue;
			if (messages != null)
				messages.add(msgKey);
			_LOG.info(e);
		}
		return rst;
	}
}
